<template>
  <a-select
    allowClear
    :style="{width:width>0?`${width}px`:'100%'}"
    :placeholder="placeholder"
    v-model="val"
    @change="onChange"
  >
    <a-select-option value="">不限</a-select-option>
    <a-select-option v-for="o in options" :key="o" :value="o">
      {{ o }}
    </a-select-option>
  </a-select>
</template>

<script>
export default {
  model: {
    prop: 'modelVal',
    event: 'change'
  },
  props: {
    modelVal: {
      type: String,
      default: ''
    },
    code: {
      type: String,
      default: ''
    },
    width: {
      type: Number,
      default: 0
    },
    placeholder: {
      type: String,
      default: '请选择'
    }
  },
  data () {
    return {
      val: '',
      options: []
    }
  },
  mounted () {
    this.val = this.modelVal
    this.search()
  },
  watch: {
    modelVal (v) {
      this.val = v
    },
    code () {
      this.search()
    }
  },
  methods: {
    search () {
      if (!this.code || this.code.length === 0) {
        return
      }
      this.$http.get('/zg-system/un/enum', { code: this.code }, data => {
        this.options = data
      })
    },
    onChange (v) {
      this.$emit('change', v)
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
